#include <stdio.h> 
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <stdint.h>

#include "key.h"
#include "sm2.h"
#include "common_func.h"
#include "core_time.h"

//BSP板级支持包所需全局变量
unsigned long UART_BASE = 0xbf000000;					//UART16550的虚地址
unsigned long CONFREG_TIMER_BASE = 0xbf20f100;			//CONFREG计数器的虚地址
unsigned long CONFREG_CLOCKS_PER_SEC = 50000000L;		//CONFREG时钟频率
unsigned long CORE_CLOCKS_PER_SEC = 33000000L;			//处理器核时钟频率

int main(int argc, char** argv)
{
	printf("\nHello Loongarch32r INT Key!\n");
	
	//delay_ms(1000);

	uint32_t ret = 0;
	
	ECCrefPrivateKey prikey = {
		.K = {0x526FF0B7, 0x5DB2D940, 0x89FA76A1, 0x983DB034, 
			  0x0D79FF37, 0xACC58E21, 0x4FAE46A3, 0xC7DAE211}};

	ECCrefPublicKey pubkey = {
		.x = {0x958E4164, 0x204F52F5, 0x4AE34032, 0x23EE5F42, 
		      0xE01ED9EB, 0xECBEE98B, 0x04424CCE, 0x1E7AABD4},
		.y = {0xB3891DE9, 0xBFB2F38F, 0x4F252C38, 0x0590F206, 
			  0xBDA63306, 0x626B646C, 0x76C1D7D9, 0x15D464F5}};
	
	ECCrefPrivateKey prikey_out;
	ECCrefPublicKey pubkey_out;
	uint8_t sm4_key_out[16];			  
			  
	uint8_t sm4_key[16] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 
					       0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
	
	char *pin = "qwer1234";
	
	//printf("Start Write Key.\n");
	
	ret = write_key(KEY_TYPE_SM2, 0, (uint8_t*)pin, &prikey, &pubkey, NULL);
	printf("Write SM2 index 0 ret = 0x%X\n", ret);
	ret = write_key(KEY_TYPE_SM4, 1, (uint8_t*)pin, NULL, NULL, sm4_key);
	printf("Write SM4 index 1 ret = 0x%X\n", ret);
	
	ret = access_key_test(KEY_TYPE_SM2, 0, (uint8_t*)pin, &prikey_out, &pubkey_out, NULL);
	printf("Access SM2 index 0 ret = 0x%X\n", ret);
	ret = access_key_test(KEY_TYPE_SM2, 1, (uint8_t*)pin, &prikey_out, &pubkey_out, NULL);
	printf("Access SM2 index 1 ret = 0x%X\n", ret);
	ret = access_key_test(KEY_TYPE_SM4, 0, (uint8_t*)pin, NULL, NULL, sm4_key_out);
	printf("Access SM4 index 0 ret = 0x%X\n", ret);
	ret = access_key_test(KEY_TYPE_SM4, 1, (uint8_t*)pin, NULL, NULL, sm4_key_out);
	printf("Access SM4 index 1 ret = 0x%X\n", ret);

	ret = delete_key(KEY_TYPE_SM2, 0, (uint8_t*)pin);
	printf("Delete SM2 index 0 ret = 0x%X\n", ret);
	ret = delete_key(KEY_TYPE_SM2, 1, (uint8_t*)pin);
	printf("Delete SM2 index 1 ret = 0x%X\n", ret);
	ret = delete_key(KEY_TYPE_SM4, 0, (uint8_t*)pin);
	printf("Delete SM4 index 0 ret = 0x%X\n", ret);
	ret = delete_key(KEY_TYPE_SM4, 1, (uint8_t*)pin);
	printf("Delete SM4 index 1 ret = 0x%X\n", ret);
	
	release_key(KEY_TYPE_SM2, 0);
	release_key(KEY_TYPE_SM4, 1);
	
	printf("INT KEY test finished!\n");
	
	while(1)
	{
		
	}
}